package technical_engineering.procedures;

import technical_engineering.block.InputChannelBlock;

import technical_engineering.TechnicalEngineeringMod;

import net.minecraftforge.energy.CapabilityEnergy;

import net.minecraft.world.IWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.tileentity.TileEntity;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.Map;

import com.google.common.collect.ImmutableMap;

public class OutputChannelBaseProcedure {
	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("x") == null) {
			if (!dependencies.containsKey("x"))
				TechnicalEngineeringMod.LOGGER.warn("Failed to load dependency x for procedure OutputChannelBase!");
			return;
		}
		if (dependencies.get("y") == null) {
			if (!dependencies.containsKey("y"))
				TechnicalEngineeringMod.LOGGER.warn("Failed to load dependency y for procedure OutputChannelBase!");
			return;
		}
		if (dependencies.get("z") == null) {
			if (!dependencies.containsKey("z"))
				TechnicalEngineeringMod.LOGGER.warn("Failed to load dependency z for procedure OutputChannelBase!");
			return;
		}
		if (dependencies.get("world") == null) {
			if (!dependencies.containsKey("world"))
				TechnicalEngineeringMod.LOGGER.warn("Failed to load dependency world for procedure OutputChannelBase!");
			return;
		}
		double x = dependencies.get("x") instanceof Integer ? (int) dependencies.get("x") : (double) dependencies.get("x");
		double y = dependencies.get("y") instanceof Integer ? (int) dependencies.get("y") : (double) dependencies.get("y");
		double z = dependencies.get("z") instanceof Integer ? (int) dependencies.get("z") : (double) dependencies.get("z");
		IWorld world = (IWorld) dependencies.get("world");
		double sx = 0;
		double sy = 0;
		double sz = 0;
		double energy = 0;
		if (MachineRedstoneRunProcedure.executeProcedure(ImmutableMap.of("x", x, "y", y, "z", z, "world", world))) {
			sx = (double) (-5);
			for (int index0 = 0; index0 < (int) (10); index0++) {
				sy = (double) (-5);
				for (int index1 = 0; index1 < (int) (10); index1++) {
					sz = (double) (-5);
					for (int index2 = 0; index2 < (int) (10); index2++) {
						if (((world.getBlockState(new BlockPos((int) (x + sx), (int) (y + sy), (int) (z + sz)))).getBlock() == InputChannelBlock.block
								.getDefaultState().getBlock())) {
							if ((new Object() {
								public boolean canReceiveEnergy(IWorld world, BlockPos pos) {
									AtomicBoolean _retval = new AtomicBoolean(false);
									TileEntity _ent = world.getTileEntity(pos);
									if (_ent != null)
										_ent.getCapability(CapabilityEnergy.ENERGY, null)
												.ifPresent(capability -> _retval.set(capability.canReceive()));
									return _retval.get();
								}
							}.canReceiveEnergy(world, new BlockPos((int) (x + sx), (int) (y + sy), (int) (z + sz))))) {
								energy = (double) (new Object() {
									public int extractEnergySimulate(IWorld world, BlockPos pos, int _amount) {
										AtomicInteger _retval = new AtomicInteger(0);
										TileEntity _ent = world.getTileEntity(pos);
										if (_ent != null)
											_ent.getCapability(CapabilityEnergy.ENERGY, null)
													.ifPresent(capability -> _retval.set(capability.extractEnergy(_amount, true)));
										return _retval.get();
									}
								}.extractEnergySimulate(world, new BlockPos((int) x, (int) y, (int) z), (int) 25));
								energy = (double) (new Object() {
									public int receiveEnergySimulate(IWorld world, BlockPos pos, int _amount) {
										AtomicInteger _retval = new AtomicInteger(0);
										TileEntity _ent = world.getTileEntity(pos);
										if (_ent != null)
											_ent.getCapability(CapabilityEnergy.ENERGY, null)
													.ifPresent(capability -> _retval.set(capability.receiveEnergy(_amount, true)));
										return _retval.get();
									}
								}.receiveEnergySimulate(world, new BlockPos((int) (x + sx), (int) (y + sy), (int) (z + sz)), (int) energy));
								{
									TileEntity _ent = world.getTileEntity(new BlockPos((int) x, (int) y, (int) z));
									int _amount = (int) energy;
									if (_ent != null)
										_ent.getCapability(CapabilityEnergy.ENERGY, null)
												.ifPresent(capability -> capability.extractEnergy(_amount, false));
								}
								{
									TileEntity _ent = world.getTileEntity(new BlockPos((int) (x + sx), (int) (y + sy), (int) (z + sz)));
									int _amount = (int) energy;
									if (_ent != null)
										_ent.getCapability(CapabilityEnergy.ENERGY, null)
												.ifPresent(capability -> capability.receiveEnergy(_amount, false));
								}
							}
						}
						sz = (double) (sz + 1);
					}
					sy = (double) (sy + 1);
				}
				sx = (double) (sx + 1);
			}
		}
	}
}
